package com.hm.rabitmq.distributing;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * @author shishun.wang
 * @date 2016年12月9日 上午11:03:23
 * @version 1.0
 * @describe 生产者,分发任务
 */
public class Task {

	private final static String QUEUE_NAME = "WORK_QUEUE";

	public static void main(String[] args) {
		try {
			send();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void send() throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("115.28.66.183");

		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();
		
		boolean durable = true; // 声明队列可持久化(消费者和生产者均需要)，解决由于消费者突然挂机消息丢失问题
		channel.queueDeclare(QUEUE_NAME, durable, false, false, null);

		// 定义15个任务
		String[] tasks = { "task1", "task2", "task3", "task4", "task5", "task6", "task7", "task8", "task9", "task10",
				"task11", "task12", "task13", "task14", "task15" };
		System.out.println("start to dispatch tasks...");
		// 分发任务
		for (String task : tasks) {
			// 设置消息持久化
			channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, task.getBytes());
			// channel.basicPublish("", QUEUE_NAME, null, task.getBytes());
		}

		connection.close();
	}
}
